package taro.todolist.jparepo;

import jakarta.transaction.Transactional;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.lang.NonNull;
import org.springframework.stereotype.Repository;
import taro.todolist.dao.Member;


@Repository
public interface MemberRepository extends JpaRepository<Member, Long> {

    @Modifying // 添加 Modifying 注解
    @Transactional // 确保在事务中执行
    @Query(
        value = "UPDATE member SET deleted = true WHERE id = :id",
        nativeQuery = true
    )
    void deleteById(@NonNull Long id);

    @Modifying
    @Transactional
    @Query(
        value = "UPDATE member SET deleted = false WHERE id = :id",
        nativeQuery = true
    )
    void recoverById(@NonNull Long id);

    @Modifying
    @Transactional
    @Query(
        value = "DELETE FROM member WHERE id = :id",
        nativeQuery = true
    )
    void purgeById(@NonNull Long id);

    @Query(
        value = "SELECT COUNT(*) > 0 FROM member WHERE id = :id",
        nativeQuery = true
    )
    Long getExistedNumberById(@NonNull Long id);

    @Query(
        value = "SELECT avatar_path FROM member where id = :id",
        nativeQuery = true
    )
    String getAvatarPathById(@NonNull Long id);

}
